﻿// -----------------------------------------------------------------------
// <copyright file="PushCountService.cs" company="Zen Design Corp">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Zen.BlackBerry.Push.Count
{
	using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
	using Microsoft.Practices.Unity;

	/// <summary>
	/// Implementation of the <see cref="IPushCountService"/> interface.
	/// </summary>
	public class PushCountService : IPushCountService
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="PushCountService"/> class.
		/// </summary>
		public PushCountService()
		{
		}

		/// <summary>
		/// Gets or sets the repository.
		/// </summary>
		/// <value>The repository.</value>
		[Dependency]
		public IPushCountRepository Repository
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the exception manager.
		/// </summary>
		/// <value>The exception manager.</value>
		[Dependency]
		public ExceptionManager ExceptionManager
		{
			get;
			set;
		}

		#region IPushCountService Members

		/// <summary>
		/// Adds a counter for the push request with the given push id.
		/// </summary>
		/// <param name="pushId">The push id.</param>
		public void Add(string pushId)
		{
			ExceptionManager.Process(
				() =>
				{
					Repository.Add(pushId);
				},
				"BPSS Count");
		}

		/// <summary>
		/// Increments the counter for the push request with the given push id
		/// by the amount indicated.
		/// </summary>
		/// <param name="pushId">The push id.</param>
		/// <param name="amount">The amount.</param>
		/// <param name="max">The max.</param>
		/// <returns>
		/// A boolean <c>true</c> if counter is equal to <paramref name="max"/>
		/// as a result of the invocation.
		/// </returns>
		public bool Increment(string pushId, int amount, int max)
		{
			return ExceptionManager.Process(
				() =>
				{
					return Repository.Increment(pushId, amount, max);
				},
				"BPSS Count");
		}

		/// <summary>
		/// Removes a counter for the push request with the given push id.
		/// </summary>
		/// <param name="pushId">The push id.</param>
		public void Remove(string pushId)
		{
			ExceptionManager.Process(
				() =>
				{
					Repository.Remove(pushId);
				},
				"BPSS Count");
		}

		#endregion
	}
}
